package com.example.codegenerator.common.config;

import com.example.codegenerator.common.constant.file.FilePathConstant;
import com.example.codegenerator.common.constant.file.PathInfoConstant;
import com.example.codegenerator.common.utils.GeneratorUtils;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

import java.util.*;

import static com.example.codegenerator.common.constant.file.PackageInfoConstant.*;

/**
 * 代码生成配置类
 *
 * @author Lv
 * @date 2023/11/11
 */
@Configuration
@ComponentScan("com.example.codegenerator")
public class GeneratorConfig {
    /**
     * 处理Mysql8与Java8数据类型转换
     *
     * @return
     */
    public Map<String, String> initTypeMap() {
        Map<String, String> typeMap = GeneratorUtils.newHashMap();
        typeMap.put("BIT", "bit");
        typeMap.put("TINYINT", "Byte");
        typeMap.put("SMALLINT", "Short");
        typeMap.put("MEDIUMINT", "Integer");
        typeMap.put("INT", "Integer");
        typeMap.put("BIGINT", "Long");
        typeMap.put("FLOAT", "Float");
        typeMap.put("DOUBLE", "Double");
        typeMap.put("DECIMAL", "BigDecimal");
        typeMap.put("DATE", "Date");
        typeMap.put("TIME", "Time");
        typeMap.put("DATETIME", "LocalDateTime");
        typeMap.put("TIMESTAMP", "Timestamp");
        typeMap.put("YEAR", "Integer");
        typeMap.put("CHAR", "String");
        typeMap.put("VARCHAR", "String");
        typeMap.put("BINARY", "byte[]");
        typeMap.put("VARBINARY", "byte[]");
        typeMap.put("TINYBLOB", "byte[]");
        typeMap.put("BLOB", "byte[]");
        typeMap.put("MEDIUMBLOB", "byte[]");
        typeMap.put("LONGBLOB", "byte[]");
        typeMap.put("TINYTEXT", "String");
        typeMap.put("TEXT", "String");
        typeMap.put("MEDIUMTEXT", "String");
        typeMap.put("LONGTEXT", "String");
        typeMap.put("ENUM", "String");
        typeMap.put("SET", "String");
        typeMap.put("JSON", "String");
        return typeMap;
    }

    /**
     * 初始化字段类型导入包
     *
     * @return
     */
    public Map<String, String> initFiledTypeImportMap() {
        Map<String, String> filedTypeImportMap = GeneratorUtils.newHashMap();
        filedTypeImportMap.put("BigDecimal", "java.math.BigDecimal");
        filedTypeImportMap.put("Date", "java.util.Date");
        filedTypeImportMap.put("Time", "java.sql.Time");
        filedTypeImportMap.put("LocalDateTime", "java.time.LocalDateTime");
        filedTypeImportMap.put("Timestamp", "java.sql.Timestamp");
        return filedTypeImportMap;
    }

    /**
     * 初始化mybatisPlus导入包
     *
     * @return
     */
    public Set<String> initMybatisPlusImportPackages() {
        return new HashSet<>(Arrays.asList(
                "com.baomidou.mybatisplus.annotation.TableName",
                "com.baomidou.mybatisplus.annotation.IdType",
                "com.baomidou.mybatisplus.annotation.TableId",
                "com.baomidou.mybatisplus.annotation.FieldFill",
                "com.baomidou.mybatisplus.annotation.TableLogic",
                "com.baomidou.mybatisplus.annotation.TableField"
        ));
    }

    /**
     * 初始化序列化导入包
     *
     * @return
     */
    public Set<String> initSerializableImportPackages() {
        return new HashSet<>(Collections.singletonList("java.io.Serializable"));
    }

    /**
     * 初始化对象包信息
     *
     * @return
     */
    public Map<String, String> initPackages() {
        Map<String, String> map = GeneratorUtils.newHashMap();
        map.put("Entity", GENERATOR_PACKAGE + ENTITY);
        map.put("ServiceImpl", GENERATOR_PACKAGE + SERVICE_IMPL);
        map.put("Service", GENERATOR_PACKAGE + SERVICE);
        map.put("facade", GENERATOR_PACKAGE + FACADE);
        map.put("Controller", GENERATOR_PACKAGE + CONTROLLER);
        map.put("provider", GENERATOR_PACKAGE + PROVIDER);
        map.put("providerImpl", GENERATOR_PACKAGE + PROVIDER_IMPL);
        map.put("apis", GENERATOR_PACKAGE + APIS);
        map.put("Mapper", GENERATOR_PACKAGE + MAPPER);
        map.put("Xml", GENERATOR_PACKAGE + MAPPER_XML);
        map.put("bo", GENERATOR_PACKAGE + BO);
        map.put("vo", GENERATOR_PACKAGE + VO);
        map.put("dto", GENERATOR_PACKAGE + DTO);
        return map;
    }

    /**
     * 获取项目路径
     *
     * @return
     */
    public static String getProjectPath() {
        return System.getProperty("user.dir");
    }

    /**
     * 初始化项目包路径
     *
     * @return
     */
    public Map<String, String> initPathInfo() {
        String prefixPath = getProjectPath() + FilePathConstant.SRC_DIR_PATH + FilePathConstant.FILE_DIR_PATH;
        Map<String, String> map = GeneratorUtils.newHashMap();
        map.put(PathInfoConstant.ENTITY_PATH, prefixPath + FilePathConstant.ENTITY_DIR_PATH);
        map.put(PathInfoConstant.CONTROLLER_PATH, prefixPath + FilePathConstant.CONTROLLER_DIR_PATH);
        map.put(PathInfoConstant.CONTROLLER_API_PATH, prefixPath + FilePathConstant.CONTROLLER_API_DIR_PATH);
        map.put(PathInfoConstant.XML_PATH, prefixPath + FilePathConstant.XML_DIR_PATH);
        map.put(PathInfoConstant.PROVIDER_IMPL_PATH, prefixPath + FilePathConstant.PROVIDER_IMPL_DIR_PATH);
        map.put(PathInfoConstant.DTO_PATH, prefixPath + FilePathConstant.DTO_DIR_PATH);
        map.put(PathInfoConstant.SERVICE_IMPL_PATH, prefixPath + FilePathConstant.SERVICE_IML_DIR_PATH);
        map.put(PathInfoConstant.SERVICE_PATH, prefixPath + FilePathConstant.SERVICE_DIR_PATH);
        map.put(PathInfoConstant.BO_PATH, prefixPath + FilePathConstant.BO_DIR_PATH);
        map.put(PathInfoConstant.VO_PATH, prefixPath + FilePathConstant.VO_DIR_PATH);
        map.put(PathInfoConstant.PROVIDER_PATH, prefixPath + FilePathConstant.PROVIDER_DIR_PATH);
        map.put(PathInfoConstant.MAPPER_PATH, prefixPath + FilePathConstant.MAPPER_DIR_PATH);
        map.put(PathInfoConstant.FACADE_PATH, prefixPath + FilePathConstant.FACADE_DIR_PATH);
        map.put(PathInfoConstant.VUE_PATH, prefixPath + FilePathConstant.VUE_DIR_PATH);
        map.put(PathInfoConstant.JS_PATH, prefixPath + FilePathConstant.JS_DIR_PATH);
        return map;
    }
}
